package com.lancoo.ccas52.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lancoo.ccas52.entity.TaskProcess;
import com.lancoo.ccas52.mapper.TaskProcessMapper;
import com.lancoo.ccas52.service.TaskProcessService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 各节点设置情况 服务实现类
 * </p>
 *
 * @author quin.song
 * @since 2022-12-22
 */
@Service
public class TaskProcessServiceImpl extends ServiceImpl<TaskProcessMapper, TaskProcess> implements TaskProcessService {

    @Resource
    private TaskProcessMapper taskProcessMapper;

    public static Map<String,Integer> coursePlanMap = new LinkedHashMap<>();
    public static Map<String,Integer> coursePreMap = new LinkedHashMap<>();
    public static Map<String,Integer> teachingTaskMap = new LinkedHashMap<>();
    public static Map<String,Integer> scheduleArrPlanMap = new LinkedHashMap<>();
    public static Map<String,Integer> courseOnlineMap = new LinkedHashMap<>();

    static {
        coursePlanMap.put("开课计划",1);
        coursePlanMap.put("开课计划-设置各校区上课的学生",1);
        coursePlanMap.put("开课计划-设置院系上课场地范围",1);
        coursePlanMap.put("开课计划-获取教学计划",1);
        coursePlanMap.put("开课计划-开课计划申请设置",1);
        coursePlanMap.put("开课计划-申请开课变更",2);
        coursePlanMap.put("开课计划-审核开课变更",1);
        coursePlanMap.put("开课计划-发布开课计划",1);

        coursePreMap.put("预选课程",1);
        coursePreMap.put("预选课程-设置开课单位处理时段",1);
        coursePreMap.put("预选课程-预选课控制",1);
        coursePreMap.put("预选课程-预选课控制-设置预选课课程",2);
        coursePreMap.put("预选课程-预选课控制-设置预选课教学班",2);
        coursePreMap.put("预选课程-确认预选课参数",2);
        coursePreMap.put("预选课程-确认预选课参数-设置预选课课程",2);
        coursePreMap.put("预选课程-确认预选课参数-设置预选课教学班",2);
        coursePreMap.put("预选课程-设置学生选课时段",1);
        coursePreMap.put("预选课程-学生预选课程",1);
        coursePreMap.put("预选课程-发布选课结果",1);

        teachingTaskMap.put("教学任务",1);
        teachingTaskMap.put("教学任务-设置开课单位处理时段",1);
        teachingTaskMap.put("教学任务-设置教学任务参数",1);
        teachingTaskMap.put("教学任务-设置教学任务参数-设置课程参数",2);
        teachingTaskMap.put("教学任务-设置教学任务参数-拆班上课",2);
        teachingTaskMap.put("教学任务-设置教学任务参数-设置课程教学班",2);
        teachingTaskMap.put("教学任务-设置教学任务参数-按照教学班调整课程参数",2);
        teachingTaskMap.put("教学任务-设置教学任务参数-设置环节参数",2);
        teachingTaskMap.put("教学任务-发布教学任务",1);

        scheduleArrPlanMap.put("课表编排",1);
        scheduleArrPlanMap.put("课表编排-课时方案规则",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-基础规则",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-禁排规则",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-禁排规则-课时禁排设置",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-禁排规则-场地禁排设置",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-高级规则",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-高级规则-课程规则",1);
        scheduleArrPlanMap.put("课表编排-设置排课规则-高级规则-教师规则",1);
        scheduleArrPlanMap.put("课表编排-编排课表",1);
        scheduleArrPlanMap.put("课表编排-确定排课结果",1);
        scheduleArrPlanMap.put("课表编排-查看学期课表",1);
        scheduleArrPlanMap.put("课表编排-查看学期课表-全校课表",1);
        scheduleArrPlanMap.put("课表编排-查看学期课表-教师课表",1);
        scheduleArrPlanMap.put("课表编排-查看学期课表-教室课表",1);
        scheduleArrPlanMap.put("课表编排-查看学期课表-班级课表",1);
        scheduleArrPlanMap.put("课表编排-发布并同步至职校执行课表",1);

        courseOnlineMap.put("在线选课",1);
        courseOnlineMap.put("在线选课-设置选课轮次",1);
        courseOnlineMap.put("在线选课-选课控制—设置禁选课学生",1);
        courseOnlineMap.put("在线选课-选课控制—调整可跨年级/专业选课的学生",1);
        courseOnlineMap.put("在线选课-选课控制—设置选课课程限制",1);
        courseOnlineMap.put("在线选课-选课控制—设置选课课程限制-设置专业/年级选课上限",1);
        courseOnlineMap.put("在线选课-选课控制—设置选课课程限制-按学生调整选课上限",1);
        courseOnlineMap.put("在线选课-设置正选学生选课时段",1);
        courseOnlineMap.put("在线选课-设置正选学生选课时段-确定选课结果",1);
        courseOnlineMap.put("在线选课-设置正选学生选课时段-发布选课结果",1);
        courseOnlineMap.put("在线选课-设置补选学生选课时段",1);
        courseOnlineMap.put("在线选课-设置退选学生选课时段",1);
        courseOnlineMap.put("在线选课-设置退选学生选课时段-学生网上退选选课-发布退选结果",1);
    }



    /**
     * @param taskId 任务id
     * @param mainFlow 计划主流程
     */
    @Override
    @Async
    public void initProcess(int taskId, Integer mainFlow) {
        List<TaskProcess> list = new ArrayList<>();
        //开课计划
        coursePlanMap.forEach((key,value)->{
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        //预选课程
        coursePreMap.forEach((key,value)->{
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        //教学任务
        teachingTaskMap.forEach((key,value)->{
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        //课程编排
        scheduleArrPlanMap.forEach((key,value)->{
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        //在线选课
        courseOnlineMap.forEach((key,value)->{
            list.add(new TaskProcess().setTaskId(taskId).setProcessName(key).setPower(value).setIsFinish(0));
        });
        taskProcessMapper.addBatch(list);
    }

    @Override
    public List<TaskProcess> getProcessList(int taskId){
        List<TaskProcess> processList = this.list(new LambdaQueryWrapper<TaskProcess>().eq(TaskProcess::getTaskId, taskId));
        return processList;

    }

    @Override
    public Boolean updateProcess(int id, int finish){
        LambdaUpdateWrapper<TaskProcess> updateWrapper = new LambdaUpdateWrapper<TaskProcess>().eq(TaskProcess::getId, id)
                .set(TaskProcess::getIsFinish, finish);
        boolean update = this.update(updateWrapper);
        return update;
    }
}
